-- @owner: @zou_jialiang050
-- @date: 2022/02/24
-- @testpoint: 每个向src表中插入数据语句执行前触发向des表中插入计数器数字  expect des表中插入数据为1，2，3

--step1:建表;expect:成功
drop table if exists t_trigger_src cascade;
drop table if exists t_trigger_des cascade;
drop sequence if exists trigger_des_seq1 cascade;
create table t_trigger_src(id1 int, id2 int, id3 int) with (storage_type=ustore);
create table t_trigger_des(id1 int, id2 int, id3 int) with (storage_type=ustore);
create sequence trigger_des_seq1;

--step2:每个向src表中插入数据语句执行前触发向des表中插入计数器数字 ;expect:成功des表中插入数据为1，2，3
create or replace function tri_truncate_func() returns trigger as
$$
declare
begin
insert into t_trigger_des values(nextval('trigger_des_seq1'),nextval('trigger_des_seq1'),nextval('trigger_des_seq1'));
return new;
end
$$ language plpgsql;
/
drop trigger if exists truncate_trigger on t_trigger_src;
create trigger truncate_trigger after insert on t_trigger_src
for each statement
execute procedure tri_truncate_func();
/
select * from t_trigger_des order by id1;
insert into t_trigger_src values(1,1,1),(2,2,2),(3,3,3);
select * from t_trigger_src order by id1;
select * from t_trigger_des order by id1;

--step3:清理环境;expect:成功
drop table if exists t_trigger_src cascade;
drop table if exists t_trigger_des cascade;
drop sequence if exists trigger_des_seq1 cascade;